﻿using Asp.Versioning;
using FlyingEye.ComeServices;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace FlyingEye.Controllers
{
    public class NotificationMessage
    {
#pragma warning disable CS8618 // 在退出构造函数时，不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
        public NotificationMessage()
#pragma warning restore CS8618 // 在退出构造函数时，不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
        {

        }

        public bool ToUsers { get; set; }

        public List<string> Users { get; set; }

        public string Message { get; set; }
    }

    [Authorize]
    [ControllerName("Message")]
    [ApiVersion("1.0")]
    [Route("api/device-app-service/v{version:apiVersion}/admin-messages")]
    public class MessageController : DefaultController
    {
        public const string Secret = "SECb38733e71346cb9353315eeef15d8f919c90d2d224d17bcb89cbe0c4cb9ccf42";
        public const string AccessToken = "52046048273d0909e3f9025f4c46de0e601caea47539b56199055f0a45cc0cc7";

        private readonly ComeNotificationService _comeNotificationService;

        public MessageController(
            ComeNotificationService comeNotificationService)
        {
            _comeNotificationService = comeNotificationService;
        }

        [HttpPost]
        public async Task<OkResult> PostMessage([FromBody] NotificationMessage message)
        {
            return await this.ActionInvokeAsync(async () =>
            {
                if (message.ToUsers)
                {
                    await _comeNotificationService.NotifyUserAsync(
                      users: message.Users,
                      title: "DeviceAppService通知",
                      message: message.Message);
                }
                else
                {
                    await _comeNotificationService.NotifyGroupAsync(
                       accessToken: AccessToken,
                       secret: Secret,
                       title: "DeviceAppService通知",
                       message: message.Message);
                }
            });
        }
    }
}
